#coding=utf-8

"""
name_list = ['alex', 'tom']
try:
	name_list[2]
except:
	print "you know it."
"""
def catch_KeyboardInterrupt():
	#KeyboardInterrupt
	try:
		int = raw_input("try again, inter ctrl+c: ")
	except KeyboardInterrupt:
		print "you catch KeyboardInterrupt"


def catch_IOError():
	try:
		f = open("file.txt")
		print f
	except:
		print "you catch IOError"

def catch_ImportError():
	try:
		print os.system('pwd')
	except:
		print "you catch importerror"

def catch_IndentationError():
	try:
		for i in range(3):
			print i
	except:
		print "you catch IndentationError"

def catch_KeyError():
	try:
		name = {'a':1, 'b':2}
		print name['ca']
	except:
		print "you catch KeyError"

def catch_NameError():
	try:
		print name
	except:
		print "you catch NameError"

def catch_SyntaxError():
	'''语法错误无法抓取'''
	try:
		for i in range(2):
			print i
	except:
		print "you catch SyntaxError"

def catch_TypeError():
	try:
		a = 'nihao'
		print "%d" % a
	except:
		print "you catch TypeError"

class Caca(Exception):
	'''自定义异常，继承Exception'''
	print "ni mei ya~"

def catch_myCaca():
	'''抓取自定义异常'''
	try:
		raise Caca
	except Caca:
		print "you catch meizu"
	else:
		print "试试看应该不会执行"
	finally:
		print "不知道是不是会执行，试试看看，放心吧，无论如何是一定要执行的"

# def catch_test():
# 	try:
# 		name = [1, 2]
# 		print name[3]
# 	except err:
# 		print "system error: ", err




if __name__ == '__main__':
	# catch_KeyboardInterrupt()
	# catch_IOError()
	# catch_ImportError()
	# catch_IndentationError()
	# catch_KeyError()
	# catch_NameError()
	# catch_SyntaxError()
	# catch_TypeError()
	# catch_myCaca()
	# catch_test()